package org.hepeng.hope.entity

import org.hibernate.annotations.ColumnDefault
import javax.persistence.Column
import javax.persistence.ConstraintMode.*
import javax.persistence.Entity
import javax.persistence.FetchType.*
import javax.persistence.ForeignKey
import javax.persistence.JoinColumn
import javax.persistence.JoinTable
import javax.persistence.ManyToMany
import javax.persistence.Table
import org.hepeng.hope.entity.Entity as E

const val ACCOUNT_TABLE_NAME = "account"

@Entity
@Table(name = ACCOUNT_TABLE_NAME)
internal data class Account(@Column(length = 50) @ColumnDefault("''") var nickname: String,
                            @Column(unique = true , length = 50 , nullable = false) var email: String
) : E() {

    @ManyToMany(fetch = EAGER)
    @JoinTable(name = ACCOUNT_ROLE_TABLE_NAME ,
            joinColumns = [JoinColumn(name = "accountId")] ,
            foreignKey = ForeignKey(name= "none" , value = NO_CONSTRAINT) ,
            inverseJoinColumns = [JoinColumn(name = "roleId")] ,
            inverseForeignKey = ForeignKey(name= "none" , value = NO_CONSTRAINT))
    lateinit var roles: Set<Role>
}